<template>
  <div class="Answer">

    <el-dialog title="回 复" :visible.sync="dialogFormVisible" :center="isCenter" width="500px">
      <el-form :model="dataForm" ref="dataForm" :rules="roleRules">
        <!--<el-form-item label="帮助类型：" prop="adviceFlag" label-width="150px">
          <el-select v-model="dataForm.adviceFlag" placeholder="请选择帮助类型">
            <el-option
              v-for="item in adviceFlagList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <el-form-item label="内容：" prop="answers" label-width="100px">
          <el-input type="textarea" v-model="dataForm.answers" autocomplete="off" :rows="5" :cols="30"></el-input>
        </el-form-item>
        <el-form-item align="center">
          <el-button @click.native="clickCancel">取 消</el-button>
          <el-button type="primary" @click.native="clickCommit">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import * as message from "@/constants/message";
  import UserApi from '@/api/user/user'
  export default {
    name: 'Answer',
    props: {
      id: {
        type: Number,
        default: null
      },
    },
    created() {
      //this.dataForm.relationId = this.props.relationId
    },
    watch:{
      id(value) {
        this.dataForm.id = value
      }
    },
    data() {
      return {
        answersType:"", //回复类型
        type:"",
        dataForm:{
          id:"",
          answers:""
        },
        adviceFlagList: [
          { label: '报告单解读', value: '1' },
          { label: '诊疗指导', value: '2' }
        ],
        dialogFormVisible: false,
        isCenter: true,
        roleRules: {
          answers: [
            { required: true, message: "请输入处理内容", trigger: "blur" },
            { max: 200, message: '内容长度不超过200个字', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      clickCancel(){
        // 事件结束后清空数据
        this.$Utils.clearData(this.dataForm);
        this.dialogFormVisible = false;
        this.dataForm.id="";
        this.dataForm.answers="";
        this.answersType="";
      },
      clickCommit() {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.$confirm(message.MSG_CONFIRM_SUBMIT, message.TITLE_PROMPT, {})
             .then(() => {
               if (this.answersType == "userAdvice"){
                 this.addUserData();
               } else if (this.answersType == "companyAdvice"){
                 this.addCompanyData();
               } else if (this.answersType == "studyAnswer"){
                 this.addStudyData();
               } else {
                console.error(error)
               }
              });
              setTimeout(() => {
                this.$emit('listData');
              }, 1000);
          }
        })
      },
      addUserData(){
        let _this = this;
        _this.dataForm.adviceId = _this.dataForm.id
        _this.dataForm.state = 1
        // 回复请求帮助
        UserApi.editUserAdvice(this.dataForm)
        .then((res) =>{
          // _this.dialogFormVisible = false;
          this.clickCancel();
          this.$message({
            type: 'success',
            message: '操作成功'
          });

        })
      },
      addCompanyData(){
        let _this = this;
        _this.dataForm.adviceId = _this.dataForm.id
        _this.dataForm.state = 1
        // 回复请求帮助
        UserApi.editCompanyAdvice(this.dataForm)
          .then((res) =>{
            // _this.dialogFormVisible = false;
            this.clickCancel();
            this.$message({
              type: 'success',
              message: '操作成功'
            });
             this.$emit('listData');
          })
      },
      handleCreate() {
        this.dialogFormVisible = true
      },
      companyAnswers() {
        // 当本组件在CompanyAdviceMangement中调用时，出发该方法
        this.answersType = "companyAdvice"
      },
      userAdvice() {
        // 当本组件在userAdvice中调用时，出发该方法
        this.answersType = "userAdvice"
      }

    }
  }
</script>
<style lang="scss">
  .variant-input {
    margin-top: 2px
  }

  .Answer .country_ul {
    position: relative;
  }

  .country_ul .country_tabs {
    float: left;
    margin-bottom: 0;
    padding-left: 0;
    list-style: none;
    margin-bottom: -4px;
  }

  .country_ul .country_tabs li {
    float: left;
    cursor: pointer;
    background-color: #eee;
    width: 60px;
    height: 35px;
    margin-right: 5px;
    border: 1px solid #ddd;
    border-radius: 6px;
  }

  .country_ul .country_tabs .active {
    background-color: #fff;
  }

  .country_ul .country_tabs li a {
    span {
      line-height: 30px;
      margin-left: 15px;
    }
  }

  .variant_btn {
    display: flex;
    justify-content: center;
    margin-top: 10px;
  }
</style>
